package com.hao.service.impl;

import com.hao.service.EasyCopyService;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EasyCopyServiceImpl implements EasyCopyService {

    private static EasyCopyServiceImpl instance;

    private EasyCopyServiceImpl() {
    }

    public static EasyCopyServiceImpl getInstance() {
        if (instance == null) {
            instance = new EasyCopyServiceImpl();
        }
        return instance;
    }

    @Override
    public String easyCopy(String pathSVN, String pathFileList, String pathOutPut) {
        StringBuffer msg = new StringBuffer();
        StringBuffer logMsg = new StringBuffer();
        //校验路径
        msg = validatePath(pathSVN, pathFileList, pathOutPut, msg);
        if (StringUtils.isNotEmpty(msg.toString())) {
            return msg.toString();
        }
        //创建日志文件，记录日志
        FileWriter logFileWriter = null;
        File logFile = new File(pathOutPut + "/easyCopy.log");
        try {
            if (!logFile.exists()) {
                logFile.createNewFile();
                logFileWriter = new FileWriter(logFile);
            }
            logFileWriter = new FileWriter(logFile, true);
            logMsg.append(new Date() + "开始复制文件！\r\n");
            List fileList = getFileList(pathFileList);
            if (fileList == null || fileList.isEmpty()) {
                logMsg.append((new Date() + "文件清单为空！\r\n"));
                return "文件清单为空";
            }
            mkdirs(fileList, pathOutPut);
            logMsg.append(copyFiles(pathSVN, fileList, pathOutPut));
        } catch (IOException e) {
            logMsg.append(e + "/r/n");
            e.printStackTrace();
        } finally {
            try {
                logMsg.append(new Date() + "结束复制文件！\r\n");
                logFileWriter.write(logMsg.toString());
                if (logFileWriter != null) {
                    logFileWriter.flush();
                    logFileWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return msg.toString();
    }

    /**
     * 校验输入项
     *
     * @param pathSVN
     * @param pathFileList
     * @param pathOutPut
     * @param msg
     * @return
     */
    private StringBuffer validatePath(String pathSVN, String pathFileList, String pathOutPut, StringBuffer msg) {
        if (StringUtils.isEmpty(pathSVN)) {
            msg.append("本地SVN路径不能为空！\r\n");
        }
        if (StringUtils.isEmpty(pathFileList)) {
            msg.append("文件清单路径不能为空！\r\n");
        }
        if (StringUtils.isEmpty(pathOutPut)) {
            msg.append("输出路径不能为空！\r\n");
        }
        if (StringUtils.isNotEmpty(msg.toString())) {
            return msg;
        }
        //校验目录、文件是否存在
        File validateFile = new File(pathSVN);
        if (!validateFile.exists()) {
            msg.append("未找到本地SVN路径！" + pathSVN + "\r\n");
        }
        validateFile = new File(pathFileList);
        if (!validateFile.exists()) {
            msg.append("未找到文件清单！" + pathFileList + "\r\n");
        }
        validateFile = new File(pathOutPut);
        if (!validateFile.exists()) {
            msg.append("未找到文件清单！" + pathOutPut + "\r\n");
        }
        return msg;
    }

    /**
     * 获取文件清单
     *
     * @param pathFileList
     * @return
     */
    private List<String> getFileList(String pathFileList) throws IOException {
        FileReader fileReader = new FileReader(pathFileList);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String filePath;
        List<String> fileList = new ArrayList<>();
        while ((filePath = bufferedReader.readLine()) != null) {
            fileList.add(filePath);
        }
        bufferedReader.close();
        fileReader.close();
        return fileList;
    }

    /**
     * 创建目录
     *
     * @param fileList
     * @throws IOException
     */
    private void mkdirs(List<String> fileList, String pathOutPut) throws IOException {
        File file;
        for (String fileName : fileList) {
            fileName = (pathOutPut + fileName).replace("\\", "/");
            file = new File(fileName.substring(0, fileName.lastIndexOf("/") + 1));
            if (!file.exists()) {
                file.mkdirs();
            }
        }
    }

    /**
     * 复制文件
     *
     * @param pathSVN
     * @param fileList
     * @param pathOutPut
     * @return
     * @throws IOException
     */
    private StringBuffer copyFiles(String pathSVN, List<String> fileList, String pathOutPut) throws IOException {
        StringBuffer logMsg = new StringBuffer();
        for (String fileName : fileList) {
            File sourceFile = new File(pathSVN + fileName);
            File targetFile = new File(pathOutPut + fileName);
            if (!targetFile.exists() && sourceFile.exists()) {
                Files.copy(sourceFile.toPath(), targetFile.toPath());
                logMsg.append("复制文件" + pathSVN + fileName + "--->" + pathOutPut + fileName + "\r\n");
            }
        }
        return logMsg;
    }
}
